<template>
  <!--批量活动索赔 只有厂端-->
  <div>
    <TopFilter ref="topfilter" labelWidth="100" :list="topFilterList" :cols="4" :collapse="true" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <FilterTable
      :isExportExcel="false"
      exportFileName="批量活动索赔审核.xlsx"
      :onCalcExportData="handleExportFun"
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      datakey="records"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <!--<el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>-->
        <multiuse-button type="primary" :click="exportHandle.bind(this)">导出</multiuse-button>
      </template>
    </FilterTable>

    <Drawer :visible.sync="visibleDetail" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <detail :maId="maId" @close="closeHandler" :handleType="handleType" />
    </Drawer>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import detail from './detail';
import { isEmpty } from '@/utils/utils';
import { listRecord, exportMaRecord,exporturl } from '@/api/repair/batchEventManagement/activityClaim';
import { dealerMixin } from '@/pages/repair/batchEventManagement/dealerMixin';
import { getAppId } from '@/assets/js/auth';

export default {
  name: 'addItem',
  mixins: [dictionary, dealerMixin],
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createTopFilterList(),
      visible: false,
      visibleDetail: false,
      fetchApi: listRecord,
      dialogTitle: '',
      fetchParams: { xhrAbort: false },
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      maId: ''
    };
  },
  components: {
    detail
  },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;

    this.getAllUsers(this.columns, ['maBy'], 'table');
    this.spliceCloum();
  },
  methods: {
    async exportHandle() {
      // let json = Object.assign({}, { CODE: 'CODE_00000132' }, this.fetchParams);
      // let res = await exporturl(json);
      // if (res.resultCode == 200) {
      //   this.$router.push('/admin/auth/oem/downloadCenter');
      // }
      const [err, params] = await this.$refs.topfilter.GET_FORM_DATA();

      await this.downHandleFile(exportMaRecord, params, '批量活动索赔审核');
    },

    // 导出处理函数
    handleExportFun(row) {
      row.maStatus = this.createDictText(row.maStatus, 1014);
      row.dataSource = this.createDictText(row.dataSource, 1015);
      row.ifOta = this.createDictText(row.ifOta, 1004);

      let ifPartName = row.ifPart == 10041001 ? '已满足' : '未满足';
      let ifTechName = row.ifTech == 10041001 ? '已满足' : '未满足';
      let ifClaimName = row.ifClaim == 10041001 ? '已满足' : '未满足';

      row.ifPart = ifPartName;
      row.ifTech = ifTechName;
      row.ifClaim = ifClaimName;

      row.maLevel = this.createDictText(row.maLevel, 1017);
    },
    spliceCloum(){
       let appId = getAppId().toLowerCase()
       if (appId == 'arcfox'){
         this.columns.splice(7,1);
       }
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          fixed: 'left',
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          render: props => {
            let obj = props.row;

            let ifClaim = isEmpty(obj['ifClaim']) ? '' : obj['ifClaim'];
            let maStatus = isEmpty(obj['maStatus']) ? '' : obj['maStatus'];

            let rst = false;
            if (ifClaim == '10041001' || maStatus == '10141004' || maStatus == '10141001' || maStatus == '10141005') {
              rst = true;
            }
            /**/
            return (
              <div>
                <el-button size="mini" type="text" disabled={rst} onClick={() => this.detailHandle(props.row)}>
                  审核
                </el-button>
              </div>
            );
          }
        },

        {
          title: '状态',
          dataIndex: 'maStatus',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1014, [10141001, 10141005]),
          dictItems: this.createDictList(1014)
          // render: props => {
          //   return <span>{this.createDictText(props.row.maStatus, 1014)}</span>;
          // }
        },
        {
          title: '数据来源',
          dataIndex: 'dataSource',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1015),
          render: props => {
            return <span>{this.createDictText(props.row.dataSource, 1015)}</span>;
          }
        },
        {
          title: '活动编号',
          dataIndex: 'maNo',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '活动主题',
          dataIndex: 'maTitle',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '活动等级',
          dataIndex: 'maLevel',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1017),
          render: props => {
            return <span>{this.createDictText(props.row.maLevel, 1017)}</span>;
          }
        },
        {
          title: '是否OTA活动',
          dataIndex: 'ifOta',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1004),
          render: props => {
            return <span>{this.createDictText(props.row.ifOta, 1004)}</span>;
          }
        },
        {
          title: '配件满足情况',
          dataIndex: 'ifPart',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1004),
          render: props => {
            let ifPartName = props.row.ifPart == 10041001 ? '已满足' : '未满足';
            return <span>{ifPartName}</span>;
          }
        },
        {
          title: '技术文件情况',
          dataIndex: 'ifTech',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1004),
          render: props => {
            let ifTechName = props.row.ifTech == 10041001 ? '已满足' : '未满足';
            return <span>{ifTechName}</span>;
          }
        },
        {
          title: '索赔信息情况',
          dataIndex: 'ifClaim',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(1004),
          render: props => {
            let ifClaimName = props.row.ifClaim == 10041001 ? '已满足' : '未满足';
            return <span>{ifClaimName}</span>;
          }
        },
        {
          title: '单车工时汇总',
          dataIndex: 'labourHour',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '主损件代码',
          dataIndex: 'partCode',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '主损件名称',
          dataIndex: 'partName',
          width: 150,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '活动开始日期',
          dataIndex: 'bDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '活动结束日期',
          dataIndex: 'eDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发布日期',
          dataIndex: 'maDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '发布人',
          dataIndex: 'maBy',
          width: 150,
          sorter: true,
          dictItems: []
        },
        {
          title: '关闭日期',
          dataIndex: 'closeDate',
          width: 150,
          sorter: true,
          dateFormat: 'yyyy-MM-dd',
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '活动编号',
          fieldName: 'maNo',
          placeholder: '活动编号...'
        },
        {
          type: 'INPUT',
          label: '活动主题',
          fieldName: 'maTitle',
          placeholder: '活动主题...'
        },
        {
          type: 'SELECT',
          label: '配件满足情况',
          fieldName: 'ifPart',
          placeholder: '请选择',
          itemList: this.createDictList(1004)
        },
        {
          type: 'SELECT',
          label: '活动状态',
          fieldName: 'maStatus',
          placeholder: '请选择',
          itemList: this.createDictList(1014, [10141001, 10141005, 10141006]) // 待下发、已下发、已关闭；
        },
        {
          type: 'RANGE_DATE',
          label: '活动起止日期',
          dateType: 'daterange',
          // initialValue: [
          //   moment(new Date())
          //     .subtract(1, 'months')
          //     .format('YYYY-MM-DD'),
          //   moment(new Date()).format('YYYY-MM-DD')
          // ],
          style: { minWidth: '200px' },
          fieldName: 'bDate|eDate'
        }
      ];
    },
    isEdit(row) {},
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

    detailHandle(row) {
      this.maId = row['maId'];
      this.handleType = 'show-1';
      this.showHandler(1);
    },
    addHandle() {
      this.handleType = 'add';
      this.showHandler();
    },
    editHandle(row) {
      this.maId = row['maId'];
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    changeHandle(val) {
      // val.bDate=moment(val['bDate']).format('YYYY-MM-DD')
      // val.eDate=moment(val['eDate']).format('YYYY-MM-DD')
      this.fetchParams = Object.assign({}, val);

      console.log(this.fetchParams);
    },
    showHandler(type = 0) {
      this.dialogTitle = `审核批量活动基本信息`;
      if (type == 0) {
        this.visible = true;
      } else {
        this.visibleDetail = true;
      }
    },

    closeHandler(type) {
      this.visible = false;
      this.visibleDetail = false;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
